﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Adf.Core.Entity;
using Adf.Core.Util;
using CYQ.Data.Table;
using Test.Service;

namespace WebApplication1.Controllers
{
    public class UserController : Controller
    {
        // GET: User
        public ActionResult Modify()
        {
            String sUserCode = RequestHelper.GetQueryString("usercode");
            MDataRow drUser = TestService.Instance().User().GetEntityWithUserCode(sUserCode);
            ViewBag.drUser = drUser;
            return View();
        }

        // GET: User
        public ActionResult ModifyPwd()
        {
            String sUserCode = RequestHelper.GetQueryString("usercode");
            MDataRow drUser = TestService.Instance().User().GetEntityWithUserCode(sUserCode);
            ViewBag.drUser = drUser;
            return View();
        }



        /// <summary>
        /// 执行
        /// 袁赛
        /// 2020-5-9
        /// </summary>
        /// <param name="doCmd"></param>
        /// <returns></returns>
        public ActionResult Execute(String doCmd)
        {
            ExeMsgInfo exeMsgInfo = new ExeMsgInfo();
            if (String.IsNullOrEmpty(doCmd))
            {
                doCmd = "";
            }
            doCmd = doCmd.ToLower();
            if (doCmd.Equals("modify"))
            {
                MDataRow mEntity = TestService.Instance().User().InitDataRow();
                mEntity.LoadFrom(true);
                mEntity.Set("usercreatedate", DateTime.Now);
                exeMsgInfo = TestService.Instance().User().Update(mEntity);
            }
            else if (doCmd.Equals("modifypwd"))
            {

                MDataRow mEntity = TestService.Instance().User().InitDataRow();
                mEntity.LoadFrom(true);
                //获得页面送来的密码
                String sOnePwd = RequestHelper.GetFormString("newpwd1");
                String sTwoPwd = RequestHelper.GetFormString("newpwd2");
                String sMd5OnePwd = Md5Helper.ToMd5(sOnePwd);
                String sMd5TwoPwd = Md5Helper.ToMd5(sTwoPwd);

                if (!sMd5TwoPwd.Equals(sMd5OnePwd))
                {
                    exeMsgInfo.RetStatus = 400;
                    exeMsgInfo.RetValue = "两次密码不一致";
                    return Json(exeMsgInfo);
                }

                mEntity.Set("userpassword", sMd5TwoPwd);
                exeMsgInfo = TestService.Instance().User().Update(mEntity);
            }
            else if (doCmd.Equals("add"))
            {

                MDataRow mEntity = TestService.Instance().User().InitDataRow();
                mEntity.LoadFrom(true);
                exeMsgInfo = TestService.Instance().User().Add(mEntity);
            }

            return Json(exeMsgInfo);
        }






    }
}